package com.dm.wallpaper.board;

import com.dm.wallpaper.library.ResourceTable;
import com.dm.wallpaper.library.database.Database;
import com.dm.wallpaper.library.items.Wallpaper;
import com.dm.wallpaper.library.utils.Log;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import static org.junit.Assert.assertEquals;

public class ExampleOhosTest {
    @Test
    public void testBundleName() {
        final String actualBundleName = AbilityDelegatorRegistry.getArguments().getTestBundleName();
        assertEquals("com.dm.wallpaper.board", actualBundleName);
        Database database = testDatabaseCreate();
        testGetAllWallpapers(database);
        testgetWallpaperByid(database, 1);
        testAddWallpapers(database, getWallpaperList());
        testGetAllWallpapers(database);
        Wallpaper wallpaper = testgetWallpaperByid(database, 8);
        testfavoriteWallpaper(database, 8, (wallpaper.getFavorite() + 1) % 2);
        testgetWallpaperByid(database, 8);
        testGetWallpapersCount(database);
        testRandom(database);
        testRandom();
    }

    public Database testDatabaseCreate() {
        Log.i("单元测试  testDatabaseCreate");
        Database database = Database.get();
        Log.i("单元测试  testDatabaseCreate  数据创建" + (database == null ? "不" : "") + "成功");
        return database;
    }

    public void testAddWallpapers(Database database, List<Wallpaper> list) {
        Log.i("单元测试  testAddWallpapers 添加数据:" + list.toString());
        database.addWallpapers(list);
        Log.i("单元测试  testAddWallpapers  数据添加完成");
    }

    public void testGetAllWallpapers(Database database) {
        Log.i("单元测试  testGetAllWallpapers  数据库中存储了:" + database.getWallpapers().size() + "张壁纸");
    }

    public void testfavoriteWallpaper(Database database, int id, int fa) {
        database.favoriteWallpaper(id, fa);
        Log.i("单元测试  testfavoriteWallpaper 修改id为" + id + "收藏状态为" + fa);
    }

    public Wallpaper testgetWallpaperByid(Database database, int id) {
        Wallpaper wallpaper = database.getWallpaperByLocalIdID(id);
        Log.i("单元测试  testGetAllWallpapers  查询当前数据:" + wallpaper.getId() + "   收藏状态:" + wallpaper.getFavorite());
        return wallpaper;
    }


    public void testGetWallpapersCount(Database database) {
        Log.i("单元测试  testGetWallpapersCount  查询当前数据条数:" + database.getWallpapersCount());
    }

    public void testRandom(Database database) {
        Wallpaper wallpaper = database.getRandomWallpaper();
        Log.i("单元测试  testGetWallpapersCount  查询当前数据:" + wallpaper.toString());
    }

    public List<Wallpaper> getWallpaperList() {
        List<Wallpaper> newWallpapers = new ArrayList();
        Wallpaper wallpaperTest = Wallpaper.Builder().id(8)
                .name("测试图片")
                .author("测试作者")
                .url("https://upload-images.jianshu.io/upload_images/2159406-780cca42d54650c0.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp")
                .thumbUrl("https://upload-images.jianshu.io/upload_images/2159406-780cca42d54650c0.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp")
                .category("测试")
                .favorite(1)
                .color(ResourceTable.Color_blue)
                .build();
        newWallpapers.add(wallpaperTest);
        return newWallpapers;
    }

    public void testRandom(){
        Log.i("单元测试  testGetWallpapersCount  查询当前数据:" +  new Random().nextDouble());
    }
}